//
//  TechDetailViewController.m
//  LPSNews
//
//  Created by Ruan on 14-10-18.
//  Copyright (c) 2014年 LPS. All rights reserved.
//

#import "CollectDetailViewController.h"
#import "UIButton+NavButton.h"
#import "SDWebImageManager.h"
#import "FilePathManager.h"
#import "DB_Collect.h"
#import "CollectDataMode.h"
#import "DetailView.h"

@interface CollectDetailViewController () <UIWebViewDelegate>

@property (nonatomic, retain) UIScrollView *scrollview;
@property (nonatomic, retain) UIImageView *backImageView;
@property (nonatomic, retain) UIActivityIndicatorView *activityView;
@property (nonatomic, retain) DB_Collect *db;
@property (nonatomic, retain) DetailView * collectDetailView;

@property (nonatomic, retain) UILabel *detailTitle;
@property (nonatomic, retain) UILabel *ptimeLabel;

@end

@implementation CollectDetailViewController

- (void)dealloc
{
    [self.collectDetailView stopLoading];
    [_detailTitle release];
    [_ptimeLabel release];
    [_db release];
    [_activityView  release];
    [_backImageView release];
    [_scrollview release];
    [_collectDetailView release];
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    

    
    // 2. 添加左侧返回按钮
    UIButton *leftButton = [UIButton addNavButtonWithNormalImageString:@"back" andHeightLightedImageString:@"back_highlighted"];
    CGSize leftButtonSzie = leftButton.currentImage.size;
    leftButton.frame = (CGRect){CGPointZero,leftButtonSzie};
    leftButton.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
    [leftButton addTarget:self action:@selector(backClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * leftBarButton = [[[UIBarButtonItem alloc] initWithCustomView:leftButton] autorelease];
    self.navigationItem.leftBarButtonItem = leftBarButton;

    
    
    // 5. 添加背景logo的图片
    UIImageView *backImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"updatabackgroudImage"]];
    backImageView.frame = self.view.bounds;
    self.backImageView = backImageView;
    
    // 6. 添加链接中的菊花
    UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    activityView.frame = CGRectMake(0, 0, 80, 80);
    activityView.center = CGPointMake(160, 170);
    activityView.backgroundColor = [UIColor lightGrayColor];
    activityView.alpha = 0.8;
    activityView.layer.cornerRadius = 5;
    activityView.layer.masksToBounds = YES;
    self.activityView = activityView;
    [activityView startAnimating];

    
    // 7. 创建scrollview 用于滚动webView
    UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    self.scrollview = scrollview;
    
    DetailView *collectDetailView = [[DetailView alloc] initWithFrame:self.view.bounds];
    collectDetailView.delegate =self;
    self.collectDetailView = collectDetailView;

    
    UILabel *detailTitle = [[UILabel alloc] init];
    UILabel *ptimeLabel = [[UILabel alloc] init];
    
    detailTitle.font = [UIFont systemFontOfSize:17];
    detailTitle.textAlignment = NSTextAlignmentCenter;
    detailTitle.lineBreakMode = NSLineBreakByCharWrapping;
    detailTitle.numberOfLines = 0;
    
    
    ptimeLabel.textAlignment = NSTextAlignmentCenter;
    ptimeLabel.font = [UIFont systemFontOfSize:10];
    ptimeLabel.textColor = [UIColor grayColor];
    
    self.detailTitle = detailTitle;
    self.ptimeLabel = ptimeLabel;
    
    [scrollview addSubview:detailTitle];
    [scrollview addSubview:ptimeLabel];
    

    
    [scrollview addSubview:collectDetailView];
    [scrollview addSubview:detailTitle];
    [scrollview addSubview:ptimeLabel];
    [self.view addSubview:scrollview];
    [backImageView addSubview:activityView];
    [self.view addSubview:backImageView];
    
    [self.view bringSubviewToFront:backImageView];

    [detailTitle release];
    [ptimeLabel release];
    [collectDetailView release];
    [backImageView release];
    [activityView release];
    [scrollview release];
  
    [self uploadLocationData];
    
}


- (void)uploadLocationData
{
    // 1. 打开数据库
    DB_Collect *db = [[DB_Collect alloc] init];
    self.db = db;
    
    CollectDataMode *collectData = [self.db selectCollectDataWithDocid:self.docid];
    // 8.将body转为HTML文件,存放在webcache路径中,方便webView显示
    NSString *htmlPathString = [FilePathManager collectorBodyPathCache:collectData.title];
   
    [collectData.body writeToFile:htmlPathString atomically:YES encoding:NSUTF8StringEncoding error:nil];
    
    // 9.设置webview里的 标题 和 时间 Label
    self.detailTitle.text = collectData.title;
    CGRect collectDetailTitleFrame = [self.detailTitle.text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 10, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:24]} context:nil];
    
    self.detailTitle.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, CGRectGetHeight(collectDetailTitleFrame));
    self.ptimeLabel.text = collectData.ptime;
    self.ptimeLabel.frame = CGRectMake(0, CGRectGetMaxY(self.detailTitle.frame) +5, [UIScreen mainScreen].bounds.size.width , 20);
    
    // 10. webview显示存放的HTML文件
    NSError *error = nil;
    NSString *html = [[NSString alloc] initWithContentsOfFile:
                      htmlPathString encoding: NSUTF8StringEncoding error:&error];
    
    [self.collectDetailView loadHTMLString:html baseURL:[NSURL URLWithString:htmlPathString]];
    
    [html release];
    [db release];
    

}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    // 1. 改变字体为原文档字体大小的85%
    [self.collectDetailView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust='85%'"];
    
    // 2. 计算文本的高度
    NSString *height_str= [self.collectDetailView stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"];
    CGFloat height = [height_str floatValue];
    
    // 3.设置webview的显示frame
    self.collectDetailView.frame =  CGRectMake(5, CGRectGetMaxY(self.ptimeLabel.frame) , [UIScreen mainScreen].bounds.size.width-5, height+50);
    // 4.设置webview滚动的范围
    self.collectDetailView.scrollView.contentSize =  CGSizeMake(self.collectDetailView.frame.size.width, height + 50);
    
    // 设置scrollview 的contentSize
    self.scrollview.contentSize = self.collectDetailView.scrollView.contentSize;
    self.scrollview.contentInset = UIEdgeInsetsMake(0, 0, 130, 0);
    
    [self.activityView stopAnimating];
    [self.activityView removeFromSuperview];
    [self.backImageView removeFromSuperview];
    [webView stopLoading];
    
}




- (void)backClick:(UIButton *)btn
{
    
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    
}




@end
